package com.mes.biz.service.impl;

import com.github.pagehelper.PageInfo;
import com.mes.biz.domain.BizConfig;
import com.mes.biz.domain.BizOrderAccessory;
import com.mes.biz.domain.BizParts;
import com.mes.biz.mapper.BizConfigMapper;
import com.mes.biz.mapper.BizOrderAccessoryMapper;
import com.mes.biz.service.BizConfigService;
import com.mes.biz.service.BizOrderAccessoryService;
import com.mes.common.core.domain.BaseEntity;
import com.mes.common.core.service.BaseServiceImpl;
import com.mes.common.exception.BizException;
import com.mes.common.utils.SecurityUtils;
import com.mes.common.utils.StringUtils;
import com.mes.system.domain.SysTenant;
import com.mes.system.service.SysTenantService;
import com.mes.system.strategy.AutoCodeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 配件信息Service业务层处理
 * @author hwg
 * @email hwg
 * @date 2023-03-27
 */
@Service
@Transactional(readOnly = true)
public class BizOrderAccessoryServiceImpl extends BaseServiceImpl<BizOrderAccessoryMapper, BizOrderAccessory> implements BizOrderAccessoryService {

    private static final Logger log = LoggerFactory.getLogger(BizOrderAccessoryServiceImpl.class);

    @Resource
    private SysTenantService sysTenantService;
    @Resource
    private AutoCodeUtil autoCodeUtil;
    /**
     * 获取单条数据
     * @param bizOrderAccessory 销售单配件信息
     * @return 销售单配件信息
     */
    @Override
    public BizOrderAccessory get(BizOrderAccessory bizOrderAccessory) {
        BizOrderAccessory dto = super.get(bizOrderAccessory);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 销售单配件信息id
     * @return 销售单配件信息
     */
    @Override
    public BizOrderAccessory get(String id) {
        BizOrderAccessory dto = super.get(id);
        return dto;
    }

    /**
     * 查询销售单配件信息列表
     * @param bizOrderAccessory 销售单配件信息
     * @return 销售单配件信息
     */
    @Override
    public List<BizOrderAccessory> findList(BizOrderAccessory bizOrderAccessory) {
        List<BizOrderAccessory> bizOrderAccessoryList = super.findList(bizOrderAccessory);
        return bizOrderAccessoryList;
    }

    /**
     * 分页查询销售单配件信息列表
     * @param bizOrderAccessory 销售单配件信息
     * @return 销售单配件信息
     */
    @Override
    public PageInfo<BizOrderAccessory> findPage(BizOrderAccessory bizOrderAccessory) {
        PageInfo<BizOrderAccessory> page = super.findPage(bizOrderAccessory);
        return page;
    }

    /**
     * 保存销售单配件信息
     * @param bizOrderAccessory
     * @return 结果
     */
    @Override
    public boolean save(BizOrderAccessory bizOrderAccessory) {
        return super.save(bizOrderAccessory);
    }

    /**
     * 删除销售单配件信息信息
     * @param bizOrderAccessory
     * @return 结果
     */
    @Override
    public boolean remove(BizOrderAccessory bizOrderAccessory) {
        return super.remove(bizOrderAccessory);
    }

    /**
     * 批量删除销售单配件信息
     * @param ids 需要删除的销售单配件信息ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteBizOrderAccessoryByIds(String[] ids) {
        return mapper.deleteBizOrderAccessoryByIds(ids, BaseEntity.DEL_FLAG_DELETE);
    }

    @Transactional(readOnly = false)
    @Override
    public BizOrderAccessory create(BizOrderAccessory bizOrderAccessory) {
        //return false;
        String tenantId = SecurityUtils.getTenantId();
        bizOrderAccessory.setTenantId(tenantId);
        if(StringUtils.isEmpty(bizOrderAccessory.getAccessoryNumber())) {
            SysTenant sysTenant = sysTenantService.get(tenantId);
            if (StringUtils.isEmpty(sysTenant.getLeader())){
                throw new BizException("请联系管理员添加配件单前缀!");
            }
            String code = autoCodeUtil.genSerialCode("PARTS_CODE",sysTenant.getLeader());
            bizOrderAccessory.setAccessoryNumber(code);
        } else {
            if(getBizOrderAccessoryByAccessoryNumber(bizOrderAccessory.getAccessoryNumber())!=null) {
                throw new BizException("配件单编号已存在!");
            }
        }
        boolean save = save(bizOrderAccessory);
        if (!save){
            throw new BizException("新增失败!");
        }
        return bizOrderAccessory;
    }

    /**
     * 修改配件单
     * @param bizOrderAccessory
     * @return
     */
    @Transactional(readOnly = false)
    @Override
    public boolean update(BizOrderAccessory bizOrderAccessory) {
        //return false;
        if(StringUtils.isEmpty(bizOrderAccessory.getAccessoryNumber())) {
            throw new BizException("配件单编号不能为空!");
        }
        BizOrderAccessory orderAccessory= getBizOrderAccessoryByAccessoryNumber(bizOrderAccessory.getAccessoryNumber());
        if(orderAccessory != null && !orderAccessory.getId().equals(bizOrderAccessory.getId()) ) {
            throw new BizException("配件单编号已存在!");
        }
        return save(bizOrderAccessory);
    }

    @Override
    public BizOrderAccessory getBizOrderAccessoryByAccessoryNumber(String accessoryNumber) {
        return mapper.getBizOrderAccessoryByAccessoryNumber(accessoryNumber);
    }

}
